package com.bdtop.dbl.ui.activities.acccount;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.bdtop.dbl.BaseActivity;
import com.bdtop.dbl.R;
import com.bdtop.dbl.server.ServerApiFactory;
import com.bdtop.dbl.server.response.BaseResponse;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.ui.view.TitleBar;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.NetworkUtils;
import com.bdtop.dbl.utils.Preferences;

public class UpdatePasswordActivity extends BaseActivity implements OnClickListener {

	private TitleBar mTitleBar;
	private EditText txPassword, txNewPassword, txRePassword;
	private Button btnUpdate;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_update_password);
		initViews();
	}

	public static final void launch(Context context) {
		Intent intent = new Intent(context, UpdatePasswordActivity.class);
		context.startActivity(intent);
	}

	private void initViews() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mTitleBar.setTitle(R.string.update_password);
		mTitleBar.setBelongActivity(this);

		txPassword = (EditText) findViewById(R.id.password);
		txNewPassword = (EditText) findViewById(R.id.new_password);
		txRePassword = (EditText) findViewById(R.id.re_password);
		btnUpdate = (Button) findViewById(R.id.btn_confirm);
		btnUpdate.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		if (check()) {
			String sign = Preferences.getLoginSign(this);
			final String password = txRePassword.getText().toString().trim();
			showWaitingDialog(R.string.opering, false, true);
			// String srcpw = Preferences.getPassword(this);
			// fix bug : srcpw 应该是用户填写的
			String srcpw = txPassword.getText().toString().trim();
			ServerApiFactory.getApiImpl().upodatePassword(sign, password, srcpw, new RequestCallback() {

				@Override
				public void onSuccess(String rawData, Object response) {
					BaseResponse baseResponse = (BaseResponse) response;
					showToast(baseResponse.getDesc());
					Preferences.savePassword(UpdatePasswordActivity.this, password);
					if (baseResponse.getStatus().equals(Constants.REQUEST_SUCCESS)) {
						finish();
					}
					dismissDialog();
				}

				@Override
				public void onError(int statusCode, Throwable e, String rawData) {
					if (NetworkUtils.isNetConnect(getApplicationContext())) {
						showToast(R.string.oper_error);
					} else {
						showToast(R.string.net_error);
					}
					dismissDialog();
				}
			});
		}
	}

	public boolean check() {
		if (txPassword.getText().toString().trim().length() != txPassword.getText().toString().length()) {
			showToast(R.string.pwd_not_has_space);
			return false;
		}

		if (txPassword.getText().toString().length() < 6) {
			if (txPassword.getText().toString().length() == 0) {
				showToast(R.string.pwd_null);
			} else {
				showToast(R.string.pwd_format);
			}
			return false;
		}

		// if
		// (!(password.getText().toString()).equals(Preferences.getPassword(this)))
		// {
		// showToast(R.string.old_pwd_format);
		// return false;
		// }

		if (txNewPassword.getText().toString().trim().length() != txNewPassword.getText().toString().length()) {
			showToast(R.string.pwd_not_has_space);
			return false;
		}

		if (txNewPassword.getText().toString().length() < 6) {
			if (txNewPassword.getText().toString().length() == 0) {
				showToast(R.string.new_pwd_null);
			} else {
				showToast(R.string.pwd_format);
			}
			return false;
		}

		if (!txNewPassword.getText().toString().equals(txRePassword.getText().toString())) {
			showToast(R.string.re_password_format);
			return false;
		}

		return true;
	}
}
